from django.db import models
from django.contrib.auth.models import AbstractUser


# Create your models here.
class UserAccount(AbstractUser):
    name = models.CharField(max_length=255, default="踏雪寻梅")
    tag_line = models.CharField(max_length=255, default="无处安放的灵魂")
    birthday = models.CharField(max_length=255, null=True)
    permission = models.CharField(
        choices=[("admin", "admin"), ("ghost", "ghost"), ("user", "user")],
        default="user",
        max_length=255,
    )
    avator = models.BinaryField(default=b"")
    view_nums = models.BigIntegerField(default=1)
    otherInfos = models.JSONField(null=True)

    def __str__(self):
        return self.username


class Tag(models.Model):
    name = models.CharField(max_length=50, unique=True)

    def __str__(self):
        return self.name


class Moment(models.Model):
    author = models.ForeignKey(UserAccount, on_delete=models.CASCADE)
    context = models.TextField()
    created_at = models.CharField(max_length=255)
    like_nums = models.CharField(max_length=255, default="0")
    view_nums = models.CharField(max_length=255, default="1")
    labels = models.ManyToManyField(Tag, related_name="tag_moments")

    def __str__(self):
        return self.context


class Blog(models.Model):
    author = models.ForeignKey(UserAccount, on_delete=models.CASCADE)
    title = models.CharField(max_length=255)
    context = models.TextField()
    toc = models.JSONField(null=True)
    desc = models.TextField()
    created_at = models.CharField(max_length=255)
    updated_at = models.CharField(max_length=255)
    blog_cover = models.BinaryField(default=b"")
    labels = models.ManyToManyField(Tag, related_name="tag_blogs")
    like_nums = models.BigIntegerField(default=0)
    view_nums = models.BigIntegerField(default=1)
    is_top = models.BooleanField(default=False)

    def __str__(self):
        return self.title
